//
//  NSDate+Extension.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/16.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit


extension NSDate {
    class func myDate (creatStr:String) ->NSDate {
        let dt = NSDateFormatter()
        dt.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        //信息本地化
        dt.locale = NSLocale(localeIdentifier: "en_US")
        let creatAtDate = dt.dateFromString(creatStr)!
        return creatAtDate
    }
    //根据时间转换成字符串
    var myDateToString:String {
        //转换成时间对象
        let dt = NSDateFormatter()
       
        //指定本地化信息
        dt.locale = NSLocale(localeIdentifier: "en_US")
       
        //对比年份
        if isThisYear(self) {
            //日历对象
            let calender = NSCalendar.currentCalendar()
            if calender.isDateInToday(self ) {
                let timeinterval = abs(self.timeIntervalSinceNow)
                if timeinterval < 60 {
                    return "刚刚"
                }else if timeinterval < 60 * 60 {
                    let result = timeinterval / 60
                    return "\(Int(result))分钟前"
                }else {
                    let result = timeinterval / 3600
                    return "\(Int(result))小时前"
                }
                
            } else if calender.isDateInYesterday(self) {
                dt.dateFormat = "昨天 HH:mm"
            }else {
                dt.dateFormat = "MM-dd HH:mm"
            }
        }else {
            dt.dateFormat = "yyyy-MM-dd HH:mm"
        }
        return dt.stringFromDate(self)
    }
    
    
    // MARK:  判断微博发送的时间 跟当前的年份的对比
    private func isThisYear(creatDate:NSDate) -> Bool {
        let dt = NSDateFormatter()
        dt.dateFormat = "yyyy"
        dt.locale = NSLocale(localeIdentifier: "en_US")
        //吧时间对象转换成字符串
        let creatYear = dt.stringFromDate(creatDate)
        //吧当前时间对象转换成字符串
        let currentYear = dt.stringFromDate(NSDate())
        
        return creatYear == currentYear
        
    }
}